<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.fsiteNo" clearable class="filter-item" style="width: 200px;" placeholder="请输入客户编号"/>
      <el-input v-model="listQuery.fplanuser" clearable class="filter-item" style="width: 200px;" placeholder="请输入计划人员"/>
      <el-date-picker v-model="listQuery.fplandate" type="datetime" placeholder="请输入计划日期"/>
      <el-date-picker v-model="listQuery.forddate" type="datetime" placeholder="请输入采购日期"/>
      <el-input v-model="listQuery.fsupno" clearable class="filter-item" style="width: 200px;" placeholder="请输入供应商编号"/>
      <el-input v-model="listQuery.fitemcount" clearable class="filter-item" style="width: 200px;" placeholder="请输入品项数"/>
      <el-input v-model="listQuery.fpiececount" clearable class="filter-item" style="width: 200px;" placeholder="请输入总件数"/>
      <el-input v-model="listQuery.ftotalamt" clearable class="filter-item" style="width: 200px;" placeholder="请输入总价"/>
      <el-input v-model="listQuery.fexplanation" clearable class="filter-item" style="width: 200px;" placeholder="请输入备注"/>
      <el-date-picker v-model="listQuery.fupddate" type="datetime" placeholder="请输入修改时间"/>
      <el-input v-model="listQuery.foperator" clearable class="filter-item" style="width: 200px;" placeholder="请输入操作员"/>
      <el-date-picker v-model="listQuery.fcheckdate" type="datetime" placeholder="请输入确认时间"/>
      <el-input v-model="listQuery.fcheckuser" clearable class="filter-item" style="width: 200px;" placeholder="请输入确认员"/>
      <el-input v-model="listQuery.fbillstate" clearable class="filter-item" style="width: 200px;" placeholder="请输入单据状态"/>
      <el-button v-permission="['purchase:head:read']" class="filter-item" :style="{background:buttonColor.lookup,color:buttonColor.color}" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button v-permission="['purchase:head:create']" class="filter-item" :style="{background:buttonColor.add,color:buttonColor.color}" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" stripe element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column :index="indexMethod" align="center" label="序" type="index" />
      <el-table-column align="right" label="客户编号" prop="fsiteNo" />
      <el-table-column align="right" label="计划人员" prop="fplanuser" />
      <el-table-column :formatter="datetimeFormat" align="right" label="计划日期" prop="fplandate" />
      <el-table-column :formatter="datetimeFormat" align="right" label="采购日期" prop="forddate" />
      <el-table-column align="right" label="供应商编号" prop="fsupno" />
      <el-table-column align="right" label="品项数" prop="fitemcount" />
      <el-table-column align="right" label="总件数" prop="fpiececount" />
      <el-table-column align="right" label="总价" prop="ftotalamt" />
      <el-table-column align="right" label="备注" prop="fexplanation" />
      <el-table-column :formatter="datetimeFormat" align="right" label="修改时间" prop="fupddate" />
      <el-table-column align="right" label="操作员" prop="foperator" />
      <el-table-column :formatter="datetimeFormat" align="right" label="确认时间" prop="fcheckdate" />
      <el-table-column align="right" label="确认员" prop="fcheckuser" />
      <el-table-column align="right" label="单据状态" prop="fbillstate" />
      <el-table-column align="center" label="操作员" prop="foperator" />
      <el-table-column align="center" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <el-button v-permission="['purchase:head:update']" :style="{background:buttonColor.edit,color:buttonColor.color}" size="mini" class="button-margin" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="['purchase:head:delete']" :style="{background:buttonColor.delete,color:buttonColor.color}" size="mini" class="button-margin" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="客户编号" prop="fsiteNo">
          <el-input v-model="dataForm.fsiteNo"/>
        </el-form-item>
        <el-form-item label="计划人员" prop="fplanuser">
          <el-input v-model="dataForm.fplanuser"/>
        </el-form-item>
        <el-form-item label="计划日期" prop="fplandate">
          <el-date-picker v-model="dataForm.fplandate" type="datetime"/>
        </el-form-item>
        <el-form-item label="采购日期" prop="forddate">
          <el-date-picker v-model="dataForm.forddate" type="datetime"/>
        </el-form-item>
        <el-form-item label="供应商编号" prop="fsupno">
          <el-input v-model="dataForm.fsupno"/>
        </el-form-item>
        <el-form-item label="品项数" prop="fitemcount">
          <el-input v-model.number="dataForm.fitemcount"/>
        </el-form-item>
        <el-form-item label="总件数" prop="fpiececount">
          <el-input v-model="dataForm.fpiececount"/>
        </el-form-item>
        <el-form-item label="总价" prop="ftotalamt">
          <el-input v-model="dataForm.ftotalamt"/>
        </el-form-item>
        <el-form-item label="备注" prop="fexplanation">
          <el-input v-model="dataForm.fexplanation"/>
        </el-form-item>
        <el-form-item label="修改时间" prop="fupddate">
          <el-date-picker v-model="dataForm.fupddate" type="datetime"/>
        </el-form-item>
        <el-form-item label="操作员" prop="foperator">
          <el-input v-model="dataForm.foperator"/>
        </el-form-item>
        <el-form-item label="确认时间" prop="fcheckdate">
          <el-date-picker v-model="dataForm.fcheckdate" type="datetime"/>
        </el-form-item>
        <el-form-item label="确认员" prop="fcheckuser">
          <el-input v-model="dataForm.fcheckuser"/>
        </el-form-item>
        <el-form-item label="单据状态" prop="fbillstate">
          <el-input v-model="dataForm.fbillstate"/>
        </el-form-item>
      </el-form>
      <el-tabs type="border-card">
        <el-tab-pane label="控制指令列表">
          <div class="filter-container">
            <el-button class="filter-item" :style="{background:buttonColor.add,color:buttonColor.color}" icon="el-icon-edit" @click="handleUpdate1()">添加控制指令</el-button><!-- v-permission="['equipment:eqtDict:create']"-->
            <el-table v-loading="listLoading" :data="dataForm.plays" element-loading-text="正在查询中。。。" border fit highlight-current-row>

              <el-table-column :index="indexMethod" align="center" label="序" type="index"/>
              <el-table-column align="center" label="行号" prop="fline"/>
              <el-table-column align="center" label="设备ID" prop="feuipmentid"/>
              <el-table-column align="center" label="采购价" prop="fbuyprice"/>
              <el-table-column align="center" label="数量" prop="fqty"/>
              <el-table-column align="center" label="合计" prop="ftotamt"/>
              <el-table-column align="center" label="备注" prop="fmemo"/>
              <el-table-column align="center" width="140" label="操作" class-name="small-padding">
                <template slot-scope="scope">
                  <el-button :style="{background:buttonColor.delete,color:buttonColor.color}" size="mini" @click="handleDelete1(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="cancel()">取消</el-button>
        <el-button v-if="dialogStatus=='create'" :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="createData">确定</el-button>
        <el-button v-else :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus1]" :visible.sync="dialogFormVisible1">
      <el-form ref="dataForm1" :rules="rules" :model="dataForm1" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:150px;">
        <el-form-item label="行号" prop="dataForm1.fline">
          <el-input v-model="dataForm1.fline"/>
        </el-form-item>
        <el-form-item label="设备ID" prop="dataForm1.feuipmentid">
          <el-input v-model="dataForm1.feuipmentid"/>
        </el-form-item>
        <el-form-item label="采购价" prop="dataForm1.fbuyprice">
          <el-input v-model="dataForm1.fbuyprice"/>
        </el-form-item>
        <el-form-item label="数量" prop="dataForm1.fqty">
          <el-input v-model="dataForm1.fqty"/>
        </el-form-item>
        <el-form-item label="合计" prop="dataForm1.ftotamt">
          <el-input v-model="dataForm1.ftotamt"/>
        </el-form-item>
        <el-form-item label="备注" prop="dataForm1.fmemo">
          <el-input v-model="dataForm1.fmemo"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="dialogFormVisible1 = false">取消</el-button>
        <el-button :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="addData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
  .button-margin {
    margin : 2px 0px;
  }
  .el-button+.el-button {
    margin-left: 1px;
  }
</style>

<script>
import { listhead, createhead, updatehead, deletehead } from '@/api/head'
import Pagination from '@/components/Pagination'
import moment from 'moment'
export default {
  name: 'STcOrdplanHead',
  components: { Pagination },
  data() {
    return {
      dataForm1: {},
      dialogFormVisible1: false,
      dialogStatus1: null,
      playsTmp: null,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fsiteNo: undefined,
        fplanuser: undefined,
        fplandate: undefined,
        forddate: undefined,
        fsupno: undefined,
        fitemcount: undefined,
        fpiececount: undefined,
        ftotalamt: undefined,
        fexplanation: undefined,
        fupddate: undefined,
        foperator: undefined,
        fcheckdate: undefined,
        fcheckuser: undefined,
        fbillstate: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      dataForm: {
        fsiteNo: undefined,
        fplanuser: undefined,
        fplandate: undefined,
        forddate: undefined,
        fsupno: undefined,
        fitemcount: undefined,
        fpiececount: undefined,
        ftotalamt: undefined,
        fexplanation: undefined,
        fupddate: undefined,
        foperator: undefined,
        fcheckdate: undefined,
        fcheckuser: undefined,
        fbillstate: undefined,
        id: undefined,
        plays: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        fsiteNo: [
          { required: true, message: '客户编号不能为空' }

        ],
        fplanuser: [
          { required: true, message: '计划人员不能为空' }

        ],
        fplandate: [
          { required: true, message: '计划日期不能为空' }

        ],
        forddate: [
          { required: true, message: '采购日期不能为空' }

        ],
        fsupno: [
          { required: true, message: '供应商编号不能为空' }

        ],
        fitemcount: [

          { type: 'number', message: '品项数必须为数字值', transform: (value) => Number(value) }

        ],
        // fpiececount: [
        //
        //   { type: 'number', message: '总件数必须为数字值',transform: (value) => Number(value) },
        //
        // ],
        ftotalamt: [

        ],
        fexplanation: [

        ],
        fupddate: [

        ],
        foperator: [

        ],
        fcheckdate: [

        ],
        fcheckuser: [

        ]
        // fbillstate: [
        //
        //   { type: 'number', message: '单据状态必须为数字值',transform: (value) => Number(value) },
        //
        // ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    datetimeFormat(row, column) {
      const date = row[column.property]
      if (date === undefined || date === null) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    getList() {
      this.listLoading = true
      listhead(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleDelete1(row) {
      const index = this.dataForm.plays.indexOf(row)
      this.dataForm.plays.splice(index, 1)
      // this.dataForm.plays.pop(row)
    },
    handleUpdate1() {
      debugger
      this.dialogStatus1 = 'update'
      this.dialogFormVisible1 = true
      // this.multipleSelection = []
      this.dataForm1 = {}
      this.$nextTick(() => {
        this.$refs['dataForm1'].clearValidate()
      })
    },
    cancel() {
      this.dialogFormVisible = false
      // this.multiple = []
      this.list.forEach(v => {
        debugger
        if (v.fdictno === this.dataForm.fdictno) {
          this.dataForm.plays = this.playsTmp
          const index = this.list.indexOf(v)
          this.list.splice(index, 1, this.dataForm)
          return
        }
      })
    },
    addData() {
      this.dialogFormVisible1 = false
      this.dataForm.plays.push(this.dataForm1)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        fsiteNo: undefined,
        fplanuser: undefined,
        fplandate: undefined,
        forddate: undefined,
        fsupno: undefined,
        fitemcount: undefined,
        fpiececount: undefined,
        ftotalamt: undefined,
        fexplanation: undefined,
        fupddate: undefined,
        foperator: undefined,
        fcheckdate: undefined,
        fcheckuser: undefined,
        fbillstate: undefined,
        id: undefined,
        plays: []
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createhead(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加采购计划主表成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.playsTmp = []
      row.plays.forEach(v => {
        this.playsTmp.push(v)
      })
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updatehead(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新采购计划主表成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除采购计划主表', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletehead(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除采购计划主表成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      })
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
